package vlms.perfTest;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import vlms.dao.PersonDAO;
import vlms.util.JDBCConnectionDriver;

public class ConnectionPoolTest {

	final static  int numberOfCalls = 1000;
	final static int noOfConn = numberOfCalls;
	public static void main(String a[])
	{
		int index = 0;
		long startTime = 0;
		int endTime = 0;
		
		System.out.println("==============Connection Pooling Performance test==============");
		try {
			new JDBCConnectionDriver("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/vlmsdb", "root", "root", noOfConn+10);
			
			startTime = System.currentTimeMillis();
			index = 0;
			System.out.println("Starting the test..");
			while (index < numberOfCalls)
			{
				Connection conn1 = DriverManager.getConnection("jdbc:mysql://localhost:3306/vlmsdb", "root", "root");
				PersonDAO p = new PersonDAO();
				p.findAll(conn1);
				index++;
				if (index % 10000 == 0)
				{
					System.out.println("Mid of  the test..");
				}
				conn1.close();
			}
			
			System.out.println("Time taken per call : "+ (float)(System.currentTimeMillis() - startTime)/ numberOfCalls);

			index = 0;
			startTime = System.currentTimeMillis();
			while (index < numberOfCalls)
			{
				Connection conn2 = DriverManager.getConnection("jdbc:user_mysql:");
				new PersonDAO().findAll(conn2);			
				index++;
				conn2.close();
			}
			System.out.println("Time taken per call : "+ (float)(System.currentTimeMillis() - startTime)/ numberOfCalls);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
